package com.samba.converter;

import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;

import com.samba.entites.Type;

public class TypeConverter implements Converter {


	public Object getAsObject(FacesContext facesContext, UIComponent uiComponent, String string) {
		
		try
		{
		 int id= Integer.parseInt(string);
		 if(id== 0)
		 {
			 throw new Exception();
		 }
		 Type t = new Type();
		 t.setLabel("");
		 t.setId(id);
		 return t;
		}
		catch(Exception e)
		{
			return null;	
		}
		
	}

	
	public String getAsString(FacesContext facesContext, UIComponent uiComponent, Object object) {
		if(object instanceof Type)
		{
		Type o = (Type) object;
		return "" + o.getId();
		}
		else
		{
			throw new IllegalArgumentException("object" + object + "of type : " + object.getClass().getName()+ "; excepted type : com.samba.entites.Type");
		}
	}

}
